package com.fs.controller.v1;

import com.fs.controller.vo.device.ReqOpenDevice;
import com.fs.controller.vo.device.RespDeviceStateOfRoom;
import com.fs.controller.vo.device.VoDoorState;
import com.fs.controller.vo.device.VoSwitchState;
import com.fs.entity.Door;
import com.fs.entity.Switch;
import com.fs.global.CustomException;
import com.fs.global.init.RoleName;
import com.fs.service.DeviceOperationService;
import com.fs.service.RoomService;
import com.fs.service.bo.device.BoAllDeviceState;
import com.fs.service.bo.device.BoDoor;
import com.fs.service.bo.device.BoSwitch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "设备控制", description = "管理员账户使用")
@RestController
@RequestMapping("/v1/device")
@RequiresRoles(RoleName.ADMIN)
@Validated
public class DeviceController {

    private final DeviceOperationService deviceService;
    private final RoomService roomService;

    public DeviceController(DeviceOperationService deviceService, RoomService roomService) {
        this.deviceService = deviceService;
        this.roomService = roomService;
    }

    @ApiOperation("获取所有设备状态")
    @GetMapping("/state")
    public RespDeviceStateOfRoom getDeviceStateByRoom(@RequestParam Integer roomId) {
//        BoAllDeviceState allState;
//        try {
//            allState = deviceService.getDeviceStatus();
//        } catch (Exception e) {
//            throw new DeviceOperationException(e.getMessage());
//        }

//        List<Integer> doorIds = roomService.findDoorIds(roomId);
//        List<Integer> switchIds = roomService.findSwitchIds(roomId);

        List<Door> doors = roomService.findDoorsOfRoom(roomId);
        List<Switch> switches = roomService.findSwitchesOfRoom(roomId);
        List<VoDoorState> doorList = doors.stream().map(door -> new VoDoorState(door.getId(), door.getName())).collect(Collectors.toList());
        List<VoSwitchState> switchList = switches.stream().map(sw->new VoSwitchState(sw.getId(),sw.getName())).collect(Collectors.toList());
        return new RespDeviceStateOfRoom(switchList, doorList);
    }

    @ApiOperation("开启设备")
    @PostMapping("/open")
    public void open(@RequestBody @Valid ReqOpenDevice req) {
        String strType = req.getType();
        try {
            DeviceType type = Enum.valueOf(DeviceType.class, strType);
            switch (type) {
                case DOOR:
                    deviceService.openDoor(req.getId());
                    break;
                case SWITCH:
                    deviceService.openSwitches(Collections.singletonList(req.getId()));
                    break;
            }
        } catch (IllegalArgumentException e) {
            throw new BadRequestException("请传入正确的设备类型");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new DeviceOperationException("开启设备失败");
        }
    }

    @ApiOperation("关闭空开")
    @PostMapping("/close")
    public void close(@RequestParam Integer id) {
        try {
            deviceService.closeSwitches(Collections.singletonList(id));
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new DeviceOperationException("关闭设备失败");
        }
    }


    private enum DeviceType {
        DOOR,
        SWITCH
    }
}
